use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};

pub fn init_project_api_json(project_dir: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let mut file_path = PathBuf::from(project_dir);
    file_path.push("api.json");

    if !file_path.exists() {
        let mut file = OpenOptions::new()
            .create(true)
            .truncate(true)
            .write(true)
            .open(&file_path)?;

        let initial_content = r#"{"apis":[]}"#;
        file.write_all(initial_content.as_bytes())?;
    } else {
        println!(
            "init_project_api_json，file_path:{:?}，该文件已经存在，跳过！！",
            file_path
        );
    }

    Ok(file_path)
}
